<?php

/**
 * Description of Bd
 *
 * @author inti
 */
class Bd extends mysqli {

  private $servidor = "";
  private $usuario = "";
  private $clave = "";
  private $baseDatos = "";

  const QUERY = 0;
  const MULTI_QUERY = 1;
  const SCALAR = 2;

  const AMBIENTE = "DESARROLLO";

  const ORIGEN_DATOS_DEFAULT = 0;
  const ORIGEN_DATOS_OTRO = 1;

  public function __construct($origenDatos) {
    switch ($origenDatos . "_" . self::AMBIENTE) {
      case self::ORIGEN_DATOS_DEFAULT . "_DESARROLLO":
        $this->servidor = "LOCALHOST";
        $this->usuario = "root";
        $this->clave = "";
        $this->baseDatos = "sitiobase";
        break;
      case self::ORIGEN_DATOS_OTRO . "_DESARROLLO":
        $this->servidor = "LOCALHOST";
        $this->usuario = "root";
        $this->clave = "";
        $this->baseDatos = "sitiobase";
        break;
      default:
        $this->servidor = "";
        $this->usuario = "";
        $this->clave = "";
        $this->basedatos = "";
        break;
    }

    @parent:: __construct($this->servidor, $this->usuario, $this->clave);
    if ($this->connect_errno != 0) {
      throw new Exception("Teni la pura zorra CTM!!!!, " . $bd->error, $bd->errno);
    }
  }

  private static function get($origenDatos) {
    $bd = new Bd($origenDatos);
    $bd->select_db($bd->baseDatos);
    return $bd;
  }

  /**
   *
   * @param string $sp
   * @param enum $tipo
   * @param string $origenDatos
   * @param string $nombreCampoValor
   * @return array 
   */
  public static function exec($sp, $tipo = self::QUERY, $origenDatos = self::ORIGEN_DATOS_DEFAULT, $nombreCampoValor = "Valor") {
    $bd = self::get($origenDatos);
    $result = null;
    switch ($tipo) {
      case self::QUERY:
        $result = $bd->query($sp);
        if ($bd->errno != 0) {
          throw new Exception("Teni la pura zorra CTM!!!!, " . $bd->error, $bd->errno);
        }
        $array = array();
        while ($row = mysqli_fetch_assoc($result)) {
          $array[] = $row;
        }
        $bd->close();
        return $array;
        break;
      case self::MULTI_QUERY:
        $result = $bd->multi_query($sp);
        $arrTablas = array();
        if ($bd->errno != 0) {
          throw new Exception("Teni la pura zorra CTM!!!!, " . $bd->error, $bd->errno);
        }
        do {
          $cur = $bd->store_result();
          if ($cur) {
            $array = array();
            while ($row = mysqli_fetch_assoc($cur)) {
              $array[] = $row;
            }
            mysqli_free_result($cur);
            $arrTablas[] = $array;
          }
        } while ($bd->next_result());
        $bd->close();
        return $arrTablas;
        break;
      case self::SCALAR:
        $result = $bd->query($sp);
        if ($bd->errno != 0) {
          throw new Exception("Teni la pura zorra CTM!!!!, " . $bd->error, $bd->errno);
        }
        $array = array();
        while ($row = mysqli_fetch_assoc($result)) {
          $array[] = $row;
        }
        $row = $array[0];
        $valor = ( is_null($row[$nombreCampoValor]) ) ? "" : $row[$nombreCampoValor];
        $bd->close();
        return $valor;
        break;
      default:
        return null;
        break;
    }
  }

}

?>
